package com.yanqu.road.dao.impl.cross.chefduel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.chefduel.ChefDuelMatch;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2DishTable;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2UserJoinData;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuelUpUser;
import com.yanqu.road.entity.activity.chefduel.process3.ChefDuel3Table;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossChefDuelDaoImpl extends TempDao {


    public boolean addCrossChefDuel3TableList(List<ChefDuel3Table> list) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCrossDb());
        String sql = "INSERT INTO t_c_activity_chefduel3_table(activityId,userId,no,openItemId,serverId,joinerData,integral,openTime,overTime,myOpenTimes) VALUES(?,?,?,?,?,?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuel3Table info : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, info.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getNo()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getOpenItemId()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getServerId()));
            para.put(i++, new DbParameter(Types.VARCHAR, info.getJoinerData()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getIntegral()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getOpenTime()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getOverTime()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getMyOpenTimes()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public boolean updateCrossChefDuel3TableList(List<ChefDuel3Table> list) {
        boolean result = false;
        String sql = "UPDATE t_c_activity_chefduel3_table SET joinerData=?,overTime=?,integral=?" +
                " WHERE activityId=? and userId = ? AND no=?";
        getDb().setConnName(DbNameStrategy.getCrossDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuel3Table data : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.VARCHAR, data.getJoinerData()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getOverTime()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getNo()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public Map<Long,Map<Integer, ChefDuel3Table>> getCrossChefDuel3TableMap(int activityId) {
        Map<Long,Map<Integer, ChefDuel3Table>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_chefduel3_table WHERE activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChefDuel3Table data = initCrossChefDuel3Table(rs);
                if(!dataMap.containsKey(data.getUserId())){
                    dataMap.put(data.getUserId(),new ConcurrentHashMap<>());
                }
                dataMap.get(data.getUserId()).put(data.getNo(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    //跨服的玩家坑位信息
    private ChefDuel3Table initCrossChefDuel3Table(ResultSet rs) throws SQLException {
        ChefDuel3Table data = new ChefDuel3Table();
        data.setActivityId(rs.getInt("activityId"));
        data.setServerId(rs.getLong("serverId"));
        data.setUserId(rs.getLong("userId"));
        data.setOpenItemId(rs.getInt("openItemId"));
        data.setNo(rs.getInt("no"));
        data.setIntegral(rs.getLong("integral"));
        data.setOpenTime(rs.getLong("openTime"));
        data.setOverTime(rs.getLong("overTime"));
        data.setJoinerData(rs.getString("joinerData"));
        data.setMyOpenTimes(rs.getInt("myOpenTimes"));
        return data;
    }


    public boolean updateJoinRelationList(List<ChefDuel2UserJoinData> list) {
        boolean result = false;
        String sql = "UPDATE t_c_activity_chefduel_relation SET joinTimes1=?,joinTimes2=?,lastJoinTime=?,otherActivityId=?" +
                " WHERE activityId=? and userId1 = ? and userId2 = ?";
        getDb().setConnName(DbNameStrategy.getCrossDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuel2UserJoinData data : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, data.getJoinTime1()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getJoinTime2()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getLastJoinTime()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getOtherActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId1()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId2()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public boolean addJoinRelationList(List<ChefDuel2UserJoinData> list) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCrossDb());
        String sql = "INSERT INTO t_c_activity_chefduel_relation(userId1,joinTimes1,userId2,joinTimes2,activityId,lastJoinTime,otherActivityId) VALUES(?,?,?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuel2UserJoinData info : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.BIGINT, info.getUserId1()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getJoinTime1()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getUserId2()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getJoinTime2()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getLastJoinTime()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getOtherActivityId()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public List<ChefDuel2UserJoinData> getChefDuel2UserJoinData(int activityId) {
        List<ChefDuel2UserJoinData> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_chefduel_relation WHERE activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChefDuel2UserJoinData data = initChefDuel2UserJoinData(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private ChefDuel2UserJoinData initChefDuel2UserJoinData(ResultSet rs) throws SQLException {
        ChefDuel2UserJoinData data = new ChefDuel2UserJoinData();
        data.setActivityId(rs.getInt("activityId"));
        data.setOtherActivityId(rs.getInt("otherActivityId"));
        data.setUserId1(rs.getLong("userId1"));
        data.setUserId2(rs.getLong("userId2"));
        data.setJoinTime1(rs.getInt("joinTimes1"));
        data.setJoinTime2(rs.getInt("joinTimes2"));
        data.setLastJoinTime(rs.getLong("lastJoinTime"));
        return data;
    }

    public Map<Long, ChefDuelUpUser> getCrossChefDuel2UserMap(int activityId) {
        Map<Long, ChefDuelUpUser> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_chefduel_user WHERE activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChefDuelUpUser data = initCrossChefDuel2User(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private ChefDuelUpUser initCrossChefDuel2User(ResultSet rs) throws SQLException {
        ChefDuelUpUser data = new ChefDuelUpUser();
        data.setActivityId(rs.getInt("activityId"));
        data.setServerId(rs.getLong("serverId"));
        data.setUserId(rs.getLong("userId"));
        data.setPos(rs.getInt("pos"));
        data.setType(rs.getInt("type"));
        data.setLastScore(rs.getLong("lastScore"));

        String forbiddenSpoon = rs.getString("forbiddenSpoon");
        data.setForbiddenSpoonList(StringUtils.isNullOrEmpty(forbiddenSpoon) ? new ArrayList<>() : JSON.parseObject(forbiddenSpoon, new TypeReference<List<Integer>>() {
        }));

        data.setCampId(rs.getInt("campId"));
        data.setRankIntegral(rs.getLong("rankIntegral"));
        data.setRankTime(rs.getLong("rankTime"));
        data.setSpoonLimit(rs.getInt("spoonLimit"));
        data.setCampRewardGet(rs.getInt("campRewardGet"));
        data.setPreRank(rs.getInt("preRank"));
        data.setSupportCampId(rs.getInt("supportCampId"));
        return data;
    }


    public boolean addCrossChefDuelUserList(List<ChefDuelUpUser> cookBoy2UserList) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCrossDb());
        String sql = "INSERT INTO t_c_activity_chefduel_user(activityId,userId,serverId,pos,type,lastScore,forbiddenSpoon,campId,rankIntegral,rankTime,spoonLimit,campRewardGet,preRank,supportCampId) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuelUpUser info : cookBoy2UserList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, info.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getServerId()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getPos()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getType()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getLastScore()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(info.getForbiddenSpoonList())));
            para.put(i++, new DbParameter(Types.INTEGER, info.getCampId()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getRankIntegral()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getRankTime()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getSpoonLimit()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getCampRewardGet()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getPreRank()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getSupportCampId()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public boolean updateCrossChefDuelUserList(List<ChefDuelUpUser> cookBoy2UserList) {
        boolean result = false;
        String sql = "UPDATE t_c_activity_chefduel_user SET forbiddenSpoon=?,campId=?,rankIntegral=?,rankTime=?,spoonLimit=?,campRewardGet=?,preRank=?,supportCampId=? " +
                " WHERE activityId=? and userId = ?";
        getDb().setConnName(DbNameStrategy.getCrossDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuelUpUser data : cookBoy2UserList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getForbiddenSpoonList())));
            para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getRankIntegral()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getRankTime()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getSpoonLimit()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getCampRewardGet()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getPreRank()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getSupportCampId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public boolean addCrossDishTableList(List<ChefDuel2DishTable> cookBoy2DishTableList) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCrossDb());
        String sql = "INSERT INTO t_c_activity_chefduel2_table(activityId,userId,no,userDishId,dishId,serverId,joinerData,integral,openTime,overTime) VALUES(?,?,?,?,?,?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuel2DishTable info : cookBoy2DishTableList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, info.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getNo()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getUserDishId()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getDishId()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getServerId()));
            para.put(i++, new DbParameter(Types.VARCHAR, info.getJoinerData()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getIntegral()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getOpenTime()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getOverTime()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public boolean updateCrossDishTableList(List<ChefDuel2DishTable> cookBoy2DishTableList) {
        boolean result = false;
        String sql = "UPDATE t_c_activity_chefduel2_table SET joinerData=?,overTime=?,integral=?" +
                " WHERE activityId=? and userId = ? AND no=?";
        getDb().setConnName(DbNameStrategy.getCrossDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuel2DishTable data : cookBoy2DishTableList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.VARCHAR, data.getJoinerData()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getOverTime()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getIntegral()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getNo()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public boolean updateCrossMatchList(List<ChefDuelMatch> cookBoyMatchList) {
        boolean result = false;
        String sql = "UPDATE t_c_activity_chefduel_table SET tableData=?,openTime=?,overTime=?" +
                " WHERE activityId=? and no=?";
        getDb().setConnName(DbNameStrategy.getCrossDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuelMatch data : cookBoyMatchList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.VARCHAR, data.getTableData()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getOpenTime()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getOverTime()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getNo()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public Map<Integer, ChefDuelMatch> getCrossMatchTableMap(int activityId) {
        Map<Integer, ChefDuelMatch> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_chefduel_table WHERE activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChefDuelMatch data = initCrossMatchTable(rs);
                dataMap.put(data.getNo(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Long,Map<Integer, ChefDuel2DishTable>> getCrossDishTableMap(int activityId) {
        Map<Long,Map<Integer, ChefDuel2DishTable>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_chefduel2_table WHERE activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChefDuel2DishTable data = initCrossDishTable(rs);
                if(!dataMap.containsKey(data.getUserId())){
                    dataMap.put(data.getUserId(),new ConcurrentHashMap<>());
                }
                dataMap.get(data.getUserId()).put(data.getNo(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    //跨服的玩家坑位信息
    private ChefDuel2DishTable initCrossDishTable(ResultSet rs) throws SQLException {
        ChefDuel2DishTable data = new ChefDuel2DishTable();
        data.setActivityId(rs.getInt("activityId"));
        data.setServerId(rs.getLong("serverId"));
        data.setUserId(rs.getLong("userId"));
        data.setDishId(rs.getInt("dishId"));
        data.setUserDishId(rs.getInt("userDishId"));
        data.setNo(rs.getInt("no"));
        data.setIntegral(rs.getLong("integral"));
        data.setOpenTime(rs.getLong("openTime"));
        data.setOverTime(rs.getLong("overTime"));
        data.setJoinerData(rs.getString("joinerData"));
        return data;
    }

    //跨服的玩家坑位信息
    private ChefDuelMatch initCrossMatchTable(ResultSet rs) throws SQLException {
        ChefDuelMatch data = new ChefDuelMatch();
        data.setActivityId(rs.getInt("activityId"));
        data.setNo(rs.getInt("no"));
        data.setTableData(rs.getString("tableData"));
        data.rankInit();
        data.setOpenTime(rs.getLong("openTime"));
        data.setOverTime(rs.getLong("overTime"));
        return data;
    }

    public boolean addCrossMatchTableList(List<ChefDuelMatch> cookBoyMatchList) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCrossDb());
        String sql = "INSERT INTO t_c_activity_chefduel_table(activityId,no,tableData,openTime,overTime) VALUES(?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ChefDuelMatch info : cookBoyMatchList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, info.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, info.getNo()));
            para.put(i++, new DbParameter(Types.VARCHAR, info.getTableData()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getOpenTime()));
            para.put(i++, new DbParameter(Types.BIGINT, info.getOverTime()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

}
